<template>
  <div class="app-container">
    <!-- 顶部控制台 -->
    <div class="filter-container">
      <el-input class="filter-item" style="width: 200px; margin-left: 0px" size="mini" placeholder="请输入认证企业名称" v-model="form.searchTitle"/>
      <el-select class="filter-item" style="width: 120px" size="mini" v-model="form.searchStatus" @change="handlePageInit">
        <el-option v-for="item in pageData.searchStatusList" :key="item.value" :label="item.label" :value="item.value"/>
      </el-select>
      <el-button type="primary" size="mini" icon="el-icon-search" @click="handlePageInit">搜索</el-button>
      <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleOpenSavePage">添加</el-button>
      <el-button type="success" size="mini" icon="el-icon-share" @click="handleBatchReleaseData" :disabled="form.selection.length < 1">批量发布</el-button>
    </div>
    <!--数据列表-->
    <div class="data-table">
      <el-table :data="responseData.tableList"
                border style="width: 100%;"
                v-loading="pageData.loading"
                element-loading-text="拼命加载中"
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"
                :header-cell-style="{ background: '#545C64', color: '#ffffff' }"
                @selection-change="handleTableSelection">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column fixed="left" prop="companyAuthId" label="数据ID" width="320" align="center"/>
        <el-table-column prop="authUserId" label="用户ID" width="320" align="center"/>
        <el-table-column prop="companyName" label="企业名称" width="180" align="center">
          <template slot-scope="scope">
            <span class="link-type" @click="handleOpenInfoPage(scope)">{{ scope.row.companyName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="companyContactWay" label="企业联系方式" width="160" align="center"/>
        <el-table-column prop="companyUserPhone" label="认证手机号" width="160" align="center"/>
        <el-table-column prop="companyStatus" label="认证状态" width="120" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.companyStatus === 0"><el-tag type="warning">未通过</el-tag></span>
            <span v-if="scope.row.companyStatus === 1"><el-tag type="success">通过</el-tag></span>
          </template>
        </el-table-column>
        <el-table-column prop="companyAuthType" label="认证类型" width="80" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.companyAuthType === 1"><el-tag type="primary">供应商</el-tag></span>
          </template>
        </el-table-column>
        <el-table-column prop="companyCreateTime" label="认证时间" width="180" align="center"/>
        <el-table-column fixed="right" label="操作" width="220" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="handleOpenEditPage(scope)">编辑</el-button>
            <el-button v-if="scope.row.companyStatus === 0" size="mini" type="success" @click="handleUpdateDataStatus(scope, 1)">发布</el-button>
            <el-button v-if="scope.row.companyStatus === 1" size="mini" type="info" @click="handleUpdateDataStatus(scope, 0)">预设</el-button>
            <el-button size="mini" type="danger" @click="handleDeleteData(scope)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页器-->
    <div style="margin-top: 20px">
      <div class="block" style="width: 100%; text-align: center">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :current-page="form.pageNum"
          :page-size="form.pageSize"
          :total="form.total"
          :page-sizes="[12, 50, 100, 200]"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  // 组件引入
  // 方法工具引入
  import { PermissionsToDetectFormUser } from '@/utils/PermissionsDetect';
  import { getPermission } from '@/utils/UserCookies';
  // 接口引入
  import { loadCmsCompanyList,batchReleaseCmsCompanyData } from "@/api/customer";
  export default {
    name: "company",
    //import引入的组件需要注入到对象中才能使用
    components: {},
    data() {
      return {
        pageData:{
          searchStatusList: [
            {label: "认证状态", value: -1},
            {label: "发布", value: 1},
            {label: "未发布", value: 0}
          ],// 发布状态预设
          loading: false
        },// 页面配置数据
        form:{
          searchTitle: "",// 标题搜索
          searchStatus: "",// 发布状态搜索
          pageNum: 1,
          pageSize: 12,
          total: 0,
          selection: []
        },// 页面表单
        formRules: {},// 表单验证
        responseData: {
          tableList: []
        },// 接口响应数据
      }
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {},
    //方法集合
    methods:{
      // 事件函数
      handlePageInit(){
        this.form.pageNum = 1;
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsCompanyList();
      },// 页面初始化事件
      handleCurrentChange(val){
        this.form.pageNum = val;// 更换页码
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsCompanyList();
      },// 页码切换事件
      handleSizeChange(val){
        this.form.pageSize = val;// 更换页面大小
        this.pageData.loading = true;// 创建加载窗口
        this.loadCmsCompanyList();
      },// 页面size切换事件
      handleTableSelection(val){
        let list = [];// 初始化多选数组
        for (let i=0; i<val.length; i++){
          list.push(val[i].companyAuthId);
        }// 遍历多选的列表，将ID拼入数组中
        this.form.selection = list;// 进行多选值的切换
      },// 表格多选事件
      handleOpenSavePage(){
        if (PermissionsToDetectFormUser(getPermission(), "500001")){
          this.$jump.push({path: "/customer/companySave"});
        }  else {
          this.$message.warning("您的权限不足，无法使用此功能，请联系管理员");
        }
      },// 打开企业新增页面
      handleBatchReleaseData(){
        if (PermissionsToDetectFormUser(getPermission(), "500005")){
          this.$confirm("是否发布当前所选数据?", "提示", {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let param = {
              companyAuthId: this.form.selection.toString(),
              companyStatus: 1
            };
            this.batchReleaseCmsCompanyData(param);
          }).catch(close => {
            console.log(close);
            this.$message.info("已取消操作");
          });
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 批量发布企业信息
      handleOpenInfoPage(scope){
        if (PermissionsToDetectFormUser(getPermission(), "500003")){
          let routeUrl = this.$router.resolve({
            path: "/customer/companyInfo",
            query: {companyAuthId: scope.row.companyAuthId}
          });
          window.open(routeUrl.href, '_blank');// 使用开启新窗口跳转
        }  else {
          // 当前权限不足，不执行操作，给与相应提示
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 打开企业信息页面
      handleOpenEditPage(scope){
        if (PermissionsToDetectFormUser(getPermission(), "500003")){
          let routeUrl = this.$router.resolve({
            path: "/customer/companyEdit",
            query: {companyAuthId: scope.row.companyAuthId}
          });
          window.open(routeUrl.href, '_blank');// 使用开启新窗口跳转
        }  else {
          // 当前权限不足，不执行操作，给与相应提示
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 打开企业修改面板
      handleUpdateDataStatus(scope ,status){
        if (PermissionsToDetectFormUser(getPermission(), "500005")){
          this.$confirm("是否更改当前所选数据的发布状态?", "提示", {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let param = {
              companyAuthId: scope.row.companyAuthId,
              companyStatus: status
            };
            this.batchReleaseCmsCompanyData(param);
          }).catch(close => {
            console.log(close);
            this.$message.info("已取消操作");
          });
        } else {
          this.$message.warning('您的权限不足，无法使用此功能，请联系管理员');
        }
      },// 更改表格数据发布状态
      handleDeleteData(scope){
        this.$message.info("该功能暂未开放，如有需要请联系管理员");
      },// 删除表格数据
      // 接口
      loadCmsCompanyList(){
        loadCmsCompanyList(this.form).then(response => {
          if (response.code === "200"){
            this.pageData.loading = false;
            this.responseData.tableList = response.data.list;
            this.form.total = response.data.total;
          } else {
            this.$message.warning(response.message);
          }
        }).catch( error =>{
          console.log(error);
          this.$message.error(error);
        })
      },// 获取企业列表
      batchReleaseCmsCompanyData(param){
        batchReleaseCmsCompanyData(param).then(response => {
          if (response.code === "200"){
            this.$message.success(param.companyStatus === 1 ? "发布成功" : "下架成功");
            this.handlePageInit();
          } else {
            this.$message.warning(response.message);
          }
        }).catch(close => {
          console.log(close);
        });
      },// 发布企业认证状态
    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created: function () {
      let _this = this;
      document.onkeydown = function(e) {
        if (!_this.pageData.loading) {
          let key = window.event.keyCode;
          if (key === 13) {
            _this.handlePageInit(); //此方法是当按下enter键后要做的动作。
          }
        }
      };
      setTimeout(function () {
        _this.handlePageInit();
      }, 800);
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {},
    beforeCreate() {}, //生命周期 - 创建之前
    beforeMount() {}, //生命周期 - 挂载之前
    beforeUpdate() {}, //生命周期 - 更新之前
    updated() {}, //生命周期 - 更新之后
    beforeDestroy() {}, //生命周期 - 销毁之前
    destroyed() {}, //生命周期 - 销毁完成
    activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
  };
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>
